package com.itheima.config;


import com.itheima.common.AuthThreadLocal;
import com.itheima.common.JwtToken;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (StringUtils.isEmpty(token)){
            System.out.println("token不存在");
            return  false;
        }
        String username = JwtToken.parse(token);
        if (username==null){
            System.out.println("token已失效");
            return false;
        }
        AuthThreadLocal.setAuth(username);

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        AuthThreadLocal.remove();
    }
}
